package com.hk.core.autoconfigure.alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.hk.commons.util.Constants;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * 支付宝配置
 *
 * @author Kevin
 * @date 2019/3/5 22:13
 */
@AutoConfiguration
@ConditionalOnClass(AlipayClient.class)
@ConditionalOnProperty(prefix = "alipay", name = "enabled", havingValue = "true")
@EnableConfigurationProperties(value = {AliPayProperties.class})
public class AliPayAutoConfiguration {

    private final AliPayProperties alipayProperties;

    public AliPayAutoConfiguration(AliPayProperties alipayProperties) {
        this.alipayProperties = alipayProperties;
    }

    @Bean
    public AlipayClient alipayClient() {
        var gatewayUrl = AliPayConstants.gateway(alipayProperties.isDev());
        return new DefaultAlipayClient(gatewayUrl, alipayProperties.getAppId(),
                alipayProperties.getPrivateKey(), alipayProperties.getFormat().name(), Constants.UTF_8,
                alipayProperties.getAlipayPublicKey(), alipayProperties.getSignType().name());
    }
}
